
package hk.ces.oxfam.patch.sql;

import hk.ces.oxfam.patch.connection.StatementFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 *
 * @author ReutyCheng
 */
public class ExcuteUtil
{
  private ExcuteUtil()
  {
  }

  public static int[] excuteBatch(Connection connection, PreparedBatch batch) throws SQLException
  {
    PreparedStatement statement = StatementFactory.getPrepareStatement(connection, batch.getSql());
    for (Object[] params : batch.getParamsList())
    {
      statement = StatementFactory.setParameters(statement, params);
      statement.addBatch();
    }

    try
    {
      return statement.executeBatch();
    }
    finally
    {
      StatementFactory.closeResource(statement);
    }
  }

  public static int excute(Connection connection, PreparedSQL preparedSQL) throws SQLException
  {
    PreparedStatement statement = StatementFactory.getPrepareStatement(connection, preparedSQL);

    try
    {
      return statement.executeUpdate();
    }
    finally
    {
      StatementFactory.closeResource(statement);
    }
  }
}
